package com.pengshun.modules.api.user.service.impl;

import com.pengshun.modules.api.mapper.PsUserInfoMapper;
import com.pengshun.modules.api.user.para.CarPara;
import com.pengshun.modules.api.user.repository.CarDrivingRepository;
import com.pengshun.modules.api.user.repository.CarOwnerRepository;
import com.pengshun.modules.api.user.service.UserCarService;
import com.pengshun.modules.api.user.service.dto.CarDrivingDto;
import com.pengshun.modules.api.user.service.dto.CarOwnerDto;
import com.pengshun.modules.api.user.service.dto.UserDriverDto;
import com.pengshun.modules.api.user.service.mapstruct.CarOwnerTodo;
import com.pengshun.modules.common.BaseResult;
import com.pengshun.modules.domain.CarDriving;
import com.pengshun.modules.domain.CarOwner;
import lombok.RequiredArgsConstructor;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

@Service
@RequiredArgsConstructor
public class UserCarServiceImpl implements UserCarService {

    private final PsUserInfoMapper userInfoMapper;
    private final CarOwnerRepository carOwnerRepository;
    private  final CarOwnerTodo carOwnerTodo;
    private  final CarDrivingRepository carDrivingRepository;
    private final MessageSource messageSource;

    @Override
    public BaseResult list(CarPara para) {
        int page = (para.getPage()) * para.getSize();
        List<CarOwnerDto> list = userInfoMapper.selectUserCarList(para.getUserId(), para.getStatus(), page < 0 ? 0 : page, para.getSize());
        return new BaseResult(0,list);
    }

    @Override
    public BaseResult update(CarOwnerDto para) {
        //一个人 一个车牌最多只能绑定一次
        List<CarOwner> carOwners=carOwnerRepository.findByUserIdAndCarNumberAndCarStatusAndStatusLessThan(para.getUserId(), para.getCarNumber(), 1,3);
        if(carOwners.size()>0){
            return new BaseResult(1,messageSource.getMessage("UserCarServiceImpl.update.same.success", null, Locale.CHINA));
        }
        if(para.getCarOwnerId()!=null){
            //说明是更新
            CarOwner carOwner=carOwnerTodo.toEntity(para);;
            carOwner.setStatus(1);
            carOwner.setCarStatus(1);
            carOwner.setApplyDate(new Date());
            carOwnerRepository.save(carOwner);
        }else{
            para.setStatus(1);
            para.setCarStatus(1);
            para.setApplyDate(new Date());
            carOwnerRepository.save(carOwnerTodo.toEntity(para));
        }
        return new BaseResult(0,messageSource.getMessage("UserCarServiceImpl.update.add.success", null, Locale.CHINA));
    }

    @Override
    public BaseResult delete(CarOwnerDto para) {
        CarOwner owner=carOwnerRepository.findById(para.getCarOwnerId()).orElseGet(CarOwner::new);
        owner.setCarStatus(0);
        carOwnerRepository.save(owner);
        //  删除车辆绑定的司机
        List<CarDriving> carDrivings=carDrivingRepository.findByCarOwnerId(para.getCarOwnerId());
        if(carDrivings.size()>0){
            carDrivingRepository.deleteAll(carDrivings);
        }
        return new BaseResult(0,messageSource.getMessage("delete.success", null, Locale.CHINA));
    }

    @Override
    public BaseResult detil(CarOwnerDto para) {
        CarOwnerDto dto=carOwnerTodo.toDto(carOwnerRepository.findById(para.getCarOwnerId()).orElseGet(CarOwner::new));
        return new BaseResult(0,dto);
    }

    @Override
    public BaseResult notBindingCarList(CarDrivingDto para) {
        List<UserDriverDto> list=new ArrayList<>();
        //查询车辆绑定的司机 关联id
        String carDrivingIds=userInfoMapper.selectCarDrivingIds(para.getCarOwnerId());
        if(para.getStatus()==1){
            if(carDrivingIds!=null){
                list=userInfoMapper.selectNotBindingCarList(para.getCarUserId(),carDrivingIds,para.getStatus(),para.getCarOwnerId());
            }
        }else {
            list=userInfoMapper.selectNotBindingCarList(para.getCarUserId(),carDrivingIds,para.getStatus(),para.getCarOwnerId());
        }
        for(UserDriverDto dto:list){
            dto.setStatus(para.getStatus());
        }

        return new BaseResult(0,list);
    }

    @Override
    public BaseResult deleteAssociated(CarOwnerDto para) {
        CarDriving driving=carDrivingRepository.findById(para.getCarDrivingId()).orElseGet(CarDriving::new);
        if(driving==null){
            return new BaseResult(0,messageSource.getMessage("UserCarServiceImpl.update.delete.success", null, Locale.CHINA));
        }
        //查询司机用车辆是否有存在的货运
        Integer inTransport=userInfoMapper.findInTransportByCarnum(driving.getCarOwnerId(),driving.getDrivingUserId());
        if(inTransport!=null&&inTransport>0){
            return new BaseResult(0,messageSource.getMessage("UserCarServiceImpl.update.delete.fail", null, Locale.CHINA));
        }
        carDrivingRepository.deleteById(para.getCarDrivingId());
        //todo  操作部加记录
        return new BaseResult(0,messageSource.getMessage("UserCarServiceImpl.update.delete.success", null, Locale.CHINA));
    }
}
